<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3
 * Time: 17:15
 */
namespace App\Models;

use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;
use Illuminate\Database\Eloquent\Model;

class GameLabel extends Model {

    static $alias_list = [];
    static $list_url = '/game/game-label';
    static $save_url = '/game/label-save';

	public function paginate()
    {
    	$all_list = $this->getApiDataList(Input::get('area'));
        $all_list = self::convGridData($all_list);

        $page_size = $total = count($all_list)?:1;

        $data_list = array_slice($all_list, ($page-1)*$page_size, $page_size);
        // extract($data);

        // $data_tmp = static::hydrate((array)$subjects);

        // $list = static::hydrate($data_tmp);

        $paginator = new LengthAwarePaginator($data_list, $total, $page_size);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    // 获取服务器数据
    public static function getApiDataList($area = null) {
        $area = intval($area);
        $url = env('APP_API_HOST'). self::$list_url;
        $url = \App\Service\Common::build_url_query($url, ['area' => $area]);
        $source_rtn = file_get_contents($url);
        $data_list = json_decode($source_rtn, true);
        $data_list = $data_list['data'] ?:[];
        return $data_list;
    }

    public static function convGridData($data_list) {
        $alias_list = self::getLabelAlias();
        $list = [];
        if (empty($data_list) ) {
            return [];
        }
        foreach ($data_list as $attr => $val) {
            $list[] = [
                'key' => $attr,
                'alias' => $alias_list[$attr],
                'state' => $val,
            ];
        }
        return $list;
    }

    public static function getLabelAlias() {
        if (!empty(self::$alias_list)) {
            return $alias_list;
        }

        $url = env('APP_API_HOST'). '/game/label-alias';
        $url = \App\Service\Common::build_url_query($url, ['area' => $area]);
        $source_rtn = file_get_contents($url);
        $data_list = json_decode($source_rtn, true);
        $alias_list = $data_list['data'] ?:[];
        return $alias_list;
    }

    // 保存数据
    public static function saveLabel($area, $data) {
        $url = env('APP_API_HOST'). self::$save_url;
        $param_arr = self::convArrayParam($data, 'data');
        $param_arr['area'] = $area;
        $new_url = \App\Service\Common::build_url_query($url, $param_arr);
        \Log::info('[GAME][LABEL][SAVE]AREA['.$area.']url['.$url.']', [$new_url, $param_arr]);
        $source_rtn = file_get_contents($new_url);
        $rtn_data = json_decode($source_rtn, true);
        return $rtn_data['ret'];
    }

    // 转成
    public static function convArrayParam($array, $field) {
        $tmp_arr = [];
        foreach ($array as $fld => $val) {
            $key = $field . '[' . $fld . ']';
            $tmp_arr[$key] = $val;
        }
        return $tmp_arr;
    }
}